package com.czl.module_user.viewmodel

import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.blankj.utilcode.util.StringUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.ImgRspBean
import com.czl.base.data.bean.RepairListBean
import com.czl.base.data.bean.RoomBean
import com.czl.base.data.bean.UserDetailBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2022/1/7 8:39
 */
class PeopleDetailViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var personTypes = arrayOf("户主", "家庭成员", "租客", "朋友")

    var peopleId = ""
    var ownerId = ""
    var peopleFrom = ObservableInt(0)
    val houseName = ObservableField("")
    val idCardPhotoUrl = ObservableField("")
    val idCardBackPhotoUrl = ObservableField("")
    val personTypeName = ObservableField("")
    var personType = ""
    val idCard = ObservableField("")
    val phone = ObservableField("")
    var gender = ObservableInt(0)
    var applyStatus = ObservableInt(0)
    val name = ObservableField("")
    val facePhotoUrl = ObservableField("")
    var id = ""

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val saveCompleteEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val deleteSuccessEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val deleteEvent: SingleLiveEvent<String> = SingleLiveEvent()
        val choiceImgEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    }

    val onImageClick: BindingCommand<Any> = BindingCommand(BindingAction {
        uc.choiceImgEvent.call()
    })

    val onAddPeopleClick: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.deleteEvent.postValue(id)
    })

    fun getUserDetail() {
        var ob = when (peopleFrom.get()) {
            0 -> model.getOwnerDetail(peopleId)
            else -> model.getApplyDetail(peopleId)
        }
        ob.compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(
                object : ApiSubscriberHelper<BaseBean<UserDetailBean>>(loadService) {
                    override fun onResult(t: BaseBean<UserDetailBean>) {
                        dismissLoading()
                        if (t.code == 200) {
                            t.data?.let {
                                id = it.id
                                name.set(it.name)
                                phone.set(it.phone)
                                idCard.set(it.idCard)
                                facePhotoUrl.set(it.facePhotoUrl)
                                it.houseName?.let {
                                    houseName.set(it)
                                }
                                it.houseCode?.let {
                                    houseName.set(it)
                                }
                                idCardPhotoUrl.set(it.idCardPhotoUrl)
                                idCardBackPhotoUrl.set(it.idCardBackPhotoUrl)
                                applyStatus.set(it.applyStatus)
                                gender.set(it.gender)
                                it.personType?.let {
                                    if (it == 0) return@let
                                    personType = it.toString()
                                    personTypeName.set(personTypes[it - 1])
                                }
                            }
                        } else {
                            showErrorToast(t.msg)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        showErrorToast(msg)
                        dismissLoading()
                    }

                }
            )
    }

    fun deleteHouseUser(id: String) {
        model.deleteHouseUser(id)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.code == 200) {
                        uc.deleteSuccessEvent.call()
                        showSuccessToast(t.msg)
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                }
            })
    }

    fun uploadHeadImg(imgSrc: String) {
        model.apply {
            uploadHeadImg("user", imgSrc, "")
                .compose(RxThreadHelper.rxSchedulerHelper(this@PeopleDetailViewModel))
                .doOnSubscribe { showLoading() }
                .subscribe(object : ApiSubscriberHelper<BaseBean<ImgRspBean>>() {
                    override fun onResult(t: BaseBean<ImgRspBean>) {
                        dismissLoading()
                        if (t.data == null) return
                        if (t.code == 200) {
                            facePhotoUrl.set(t.data?.url)
                            uploadApplyFace()
                        } else {
                            showErrorToast(t.msg)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        dismissLoading()
                        showErrorToast(msg)
                    }

                })
        }
    }

    fun uploadApplyFace() {
        model.apply {
            uploadApplyFace(
                mapOf(
                    "areaId" to getAreaId(),
                    "faceUrl" to facePhotoUrl.get().orEmpty(),
                    "ownerId" to ownerId,
                )
            )
                .compose(RxThreadHelper.rxSchedulerHelper(this@PeopleDetailViewModel))
                .doOnSubscribe { showLoading() }
                .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                    override fun onResult(t: BaseBean<Any?>) {
                        dismissLoading()
                        if (t.data == null) return
                        if (t.code == 200) {

                        } else {
                            showErrorToast(t.msg)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        dismissLoading()
                        showErrorToast(msg)
                    }

                })
        }
    }

}